require("../less/coursetraining.less");
require("../lib/iconfont/iconfont.css");
document.ready(function () {
    /* 运动id */
    let id = sessionStorage.getItem("cid")
    /* 设置运动时间 */
    let sporttime = 0;
    /* 配置ajax接口 */
    let userId = window.$utils.istongguo();
    let index = 0;
    let fragments = JSON.parse(sessionStorage.getItem("fragments"))
    /* 渲染视频 */
    console.log(fragments);
    let videoUrl = document.querySelector("video");
    console.dir(videoUrl);
    xuanran();

    /* 判断播放时间 */
    //let zsporttime = sporttime%60;

    /* 判断播放的时间 */
    let stoptime = setInterval(function () {
        if (videoUrl.play()) {
            sporttime++

            console.log(sporttime)
        }
    }, 1000)

    /* 渲染的函数 */
    function xuanran() {
        videoUrl.src = "http://139.9.177.51:8099" + fragments[index].videoUrl;
        document.querySelector("#title").textContent = fragments[index].title;
        videoUrl.play()
    }
    document.querySelector("#num").textContent = 1;
    /* 默认渲染头像 */
    document.querySelector("#touxiang1").src = "http://139.9.177.51:8099" + fragments[index].imgUrl;
    document.querySelector("#title03").textContent = fragments[index].title;
    document.querySelector("#proVideo").addEventListener("click", function () {
        index--;
        if (index === -1) index = fragments.length - 1;
        xuanran()
        document.querySelector("#num").textContent = index + 1;
        document.querySelector("#touxiang1").src = "http://139.9.177.51:8099" + fragments[index].imgUrl;
        document.querySelector("#title03").textContent = fragments[index].title;
    });

    document.querySelector("#swith").addEventListener("click", function () {



    });

    document.querySelector("#nextVideo").addEventListener("click", function () {
        index++;
        if (index === fragments.length) index = 0;
        document.querySelector("#num").textContent = index + 1;
        document.querySelector("#touxiang1").src = "http://139.9.177.51:8099" + fragments[index].imgUrl;
        xuanran();
        document.querySelector("#title03").textContent = fragments[index].title;
    });


    /* 播放监听 如果一个视频停止的话就进行下一个视频的播放*/
    videoUrl.addEventListener("ended", function () {
        document.querySelector("#nextVideo").click();
    })
    /* 点击暂停按钮  弹出模态框 */

    document.querySelector("#swith").addEventListener("click", function () {
        document.querySelector(".motai").style.display = 'block';
        videoUrl.pause();
        clearInterval(jishiqi);
        clearInterval(stoptime)

    })
    document.querySelector("#jixu").addEventListener("click", function () {
        document.querySelector(".motai").style.display = 'none';
        videoUrl.play();
        setInterval(function () {
            if (videoUrl.play()) {
                sporttime++

                console.log(sporttime)
            }
        }, 1000)

        let jishiqi = setInterval(function () {
            /* 进度条 */
            /* 获取当前走的时间 */

            let currentTime = videoUrl.currentTime
            // console.log(currentTime)

            /* 获取总的时间 */
            let totalTime = videoUrl.duration;



            /* 获取屏幕宽度 */

            let offsetWidth = document.body.offsetWidth
            // console.log(offsetWidth)

            /* 求取他每次走的距离 */

            let settime = offsetWidth * currentTime / totalTime;

            document.querySelector("#jinduJs").style.width = settime + "px";
        }, 50)

    })



    let jishiqi = setInterval(function () {
        /* 进度条 */
        /* 获取当前走的时间 */
        let currentTime = videoUrl.currentTime
        // console.log(currentTime)

        /* 获取总的时间 */
        let totalTime = videoUrl.duration;



        /* 获取屏幕宽度 */

        let offsetWidth = document.body.offsetWidth
        // console.log(offsetWidth)

        /* 求取他每次走的距离 */

        let settime = offsetWidth * currentTime / totalTime;

        document.querySelector("#jinduJs").style.width = settime + "px";
    }, 50)

    /* 点击结束 */
    document.querySelector("#jieshu").addEventListener("click", function () {


        /* 发送ajax请求 */

        let amdajax = {
            userId: userId,
            id: id,
            takeTime: sporttime,
            calorie: sporttime * 3,
        }
        console.log(amdajax)
        window.$http.post("/sports/saveTrain", amdajax, function (res) {
            console.log(res);
            if (res.status === 0) {
                window.$utils.tips("发送成功","😁",2000)
                setTimeout(function(){
                  location.href = "home.html";  
                },2000)
                
            }
        })
    })


})